package com.tchepannou.limbe.mail;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import java.io.File;

/**
 * User: herve
 * Date: 13-10-21 2:28 PM
 */
public class FileAttachment implements Attachment
{
    private File _file;

    public FileAttachment (File file)
    {
        _file = file;
    }

    @Override
    public MimeBodyPart toBodyPart() throws MessagingException
    {
        MimeBodyPart part = new MimeBodyPart();
        DataSource source = new FileDataSource(_file);
        part.setDataHandler(new DataHandler(source));
        part.setFileName(_file.getName ());
        return part;
    }
}
